package com.pvo.cfg;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.jdom.Document;
import org.jdom.Element;
import org.jdom.JDOMException;
import org.jdom.input.SAXBuilder;

import com.tmp.util.PvoUtil;

public class PvocfgConfig {
	
	
	private static final String Attr_entityName = "entityName";
	private static final String Attr_dtoName = "dtoName";
	private static final String Attr_pkGenMethod = "pkGenMethod";
	private static final String Attr_mapTableName = "mapTableName";
	private static  List<Pvocfg> pvocfgList = null;
	
	static {
		pvocfgList = parsePvocfgs();

	}
	
	public static List<Pvocfg> getPvocfgList() {
		return pvocfgList;
	}


	@SuppressWarnings("unchecked")
	private static List<Pvocfg>  parsePvocfgs() {
		SAXBuilder builder = new SAXBuilder();
		Document doc = null;
		try {
			doc = builder.build(new File("src/main/resources/pvocfgs.xml"));
		} catch (JDOMException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		Element pvocfgsEle = doc.getRootElement();
		List<Element> childEles = pvocfgsEle.getChildren();
		List<Pvocfg> list = new ArrayList<Pvocfg>();
		if (childEles != null && childEles.size() > 0) {
			for (Element pvocfgEle : childEles) {
				Pvocfg pvocfg = new Pvocfg();
				String table = pvocfgEle.getTextTrim();
				pvocfg.setTable(table);
				String entityName = pvocfgEle.getAttributeValue(Attr_entityName);
				pvocfg.setEntityName(entityName);
				if (StringUtils.isEmpty(entityName)) {
					pvocfg.setEntityName(PvoUtil.getEntityNameByTableName(table));
				}
				String dtoName = pvocfgEle.getAttributeValue(Attr_dtoName);
				pvocfg.setDtoName(dtoName);
				if (StringUtils.isEmpty(dtoName)) {
					pvocfg.setDtoName(PvoUtil.getDTONameByTableName(table));
				}
				String pkGenMethod = pvocfgEle.getAttributeValue(Attr_pkGenMethod);
				pvocfg.setPkGenMethod(pkGenMethod);
				String mapTableName = pvocfgEle.getAttributeValue(Attr_mapTableName);
				if (StringUtils.isNotEmpty(mapTableName)) {
					pvocfg.setMapTableName(mapTableName);
				} else {
					pvocfg.setMapTableName(table);
				}
				list.add(pvocfg);
			}
		}
		return list;
	}
	

	public static void main(String[] args) {
		System.out.println(pvocfgList);
	}
}
